//用内存模拟redis数据库管理高频读写的数据
import db from './mysqlHelper.js';
let   Devcies=new Map();


function setDevice(device){
    Devcies.set(device.sn,device);
}

function getDevice(sn){
    return Devcies.get(sn);
}

function getDevices(){
    return Devcies;
}

function deleteDevice(sn){
    Devcies.delete(sn);
}

async function init(){
    Devcies.clear();
    let devices =await db.findAllNoPage('device');
    //console.log('初始化设备信息',JSON.stringify(devices));
    for (const device of devices) {
        let project_cfg=await db.findOne('project_cfg',{id:device.project_id});
        let interval=122;
        if(project_cfg){
           if(project_cfg.interval){
               interval=project_cfg.interval;
           }
        }
        Devcies.set(device.sn,{sn:device.sn,model:device.model,name:device.name,interval,addr:device.addr,project_id:  device.project_id});
    }
   // console.log('初始化设备信息',Devcies);
}
init();

export default { setDevice,getDevice,getDevices,deleteDevice,Devcies}